const router = require('express').Router()
const boom = require('boom')
const userRouter = require('./user')
const {CODE_ERROR} = require('../utils/constant')

router.get('/', function (req, res, next) {
    res.send('welcome')
})

router.use('/user', userRouter)


router.use(function (req, res, next) {
    console.log('404....')
    next(boom.notFound('接口不存在'))
})

router.use(function (err, req, res, next) {
    console.log(err)
    const msg = (err && err.message) || '系统错误'
    const statusCode = (err.output && err.output.statusCode) || 500;
    const errorMsg = (err.output && err.output.payload && err.output.payload.error) || err.message
    res.status(statusCode).json({
        code: CODE_ERROR,
        msg,
        error: statusCode,
        errorMsg
    })
})

module.exports = router
